-- UIShareSelect
-- Created by chengb Jun/23/2016
-- 分享选择界面

require "game/ui/form/share/UIShare";

UIShareSelect = class("UIShareSelect", function()
    return cc.Layer:create();
end);

function UIShareSelect.create(shareType, title, desc, args)
    return UIShareSelect.new(shareType, title, desc, args);
end

-- 构造函数
function UIShareSelect:ctor(shareType, title, desc, args)
    self:setName("UIShareSelect");
    local node = cc.CSLoader:createNode("layout/share/ShareSelect.csb");
    self:addChild(node);
    self.node = node;

    self.shareType = shareType;
    self.title = title;
    self.desc = desc;
    self.args = args;

    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIShareSelect:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 重绘
function UIShareSelect:redraw()
    local titleLabel = findChildByName(self.node, "CT/title");
    TextStyleM.setTitleStyle(titleLabel);
    titleLabel:setString(getLocStr("system_share"));
end

-- 注册点击事件
function UIShareSelect:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            closeFormByName("UIShareSelect");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册facebook分享按钮点击事件
    local btnFacebook = findChildByName(self.node, "CT/btn_fb");
    local fbImg = findChildByName(btnFacebook, "icon");
    fbImg:loadTexture("images/ui/channel/fb_share_01.png");
    local function onFacebookClick(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            fbImg:loadTexture("images/ui/channel/fb_share_02.png");
        elseif eventType == ccui.TouchEventType.ended then
            fbImg:loadTexture("images/ui/channel/fb_share_01.png");
            AudioM.playFx("button_return");

            -- 如果是游戏分享，直接弹sdk界面
            if self.shareType == SHARE_TYPE_GAME then
                local url = getLocStr("fb_share_url");
                local imageUrl = FormulaM.invoke("GET_FB_SHARE_IMAGE_URL", self.shareType);
                showFBShareDialog(self.title, self.desc, url, imageUrl, self.shareType);
                closeFormByName("UIShareSelect");
                return;
            end

            local uiShare = UIShare.create(SHARE_WAY_FACEBOOK, self.shareType, self.title, self.desc, self.args);
            UIMgr.getCurrentScene():removeFormByName("UIShare");
            UIMgr.getCurrentScene():addForm(uiShare);
            closeFormByName("UIShareSelect");
        end
    end
    btnFacebook:addTouchEventListener(onFacebookClick);
    TextStyleM.setTextStyle(btnFacebook, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnFacebook:setTitleText(getLocStr("btn_text_fb_share"));

    -- 注册line分享按钮点击事件
    local btnLine = findChildByName(self.node, "CT/btn_line");
    local lineImg = findChildByName(btnLine, "icon");
    lineImg:loadTexture("images/ui/channel/line_share_01.png");
    local function onFacebookClick(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            lineImg:loadTexture("images/ui/channel/line_share_02.png");
        elseif eventType == ccui.TouchEventType.ended then
            lineImg:loadTexture("images/ui/channel/line_share_01.png");
            AudioM.playFx("button_return");

            -- 如果是游戏分享，直接打开链接
            if self.shareType == SHARE_TYPE_GAME then
                local url = FormulaM.invoke("GET_LINE_SHARE_URL", self.shareType);
                openUrl(url);
                EventMgr.fire(event.LINE_SHARE_SUCC, { ["shareType"] = self.shareType });
                closeFormByName("UIShareSelect");
                return;
            end

            local uiShare = UIShare.create(SHARE_WAY_LINE, self.shareType, self.title, self.desc, self.args);
            UIMgr.getCurrentScene():removeFormByName("UIShare");
            UIMgr.getCurrentScene():addForm(uiShare);
            closeFormByName("UIShareSelect");
        end
    end
    btnLine:addTouchEventListener(onFacebookClick);
    TextStyleM.setTextStyle(btnLine, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnLine:setTitleText(getLocStr("btn_text_line_share"));
end

-- 注册事件处理回调函数
function UIShareSelect:registerEventCallback()

    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UIShareSelect 界面析构清理");
            EventMgr.removeAll("UIShareSelect");
        end
    end);

end




